layui.use(['layer', 'form', 'table', 'ztree', 'laydate','laypage', 'admin', 'ax', 'func', 'tree', 'util'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ZTree = layui.ztree;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var laypage = layui.laypage;
    var admin = layui.admin;
    var func = layui.func;
    var tree = layui.tree;
    var util = layui.util;


    /**
     * 系统管理--用户管理
     */
    var MgrUser = {
        tableId: "userTable",    //表格id
        condition: {
            name: "",
            deptId: "",
            timeLimit: ""
        }
    };

    /**
     * 遍历获取当前所有框架
     */
    var count1;
    var pageSize = 1;

    var queryData = {};
    test();

    /**
     * 遍历获取当前所有日程
     */
    function loadSchedule(dataa){
        // if (dataa==null){
        //     dataa ={}
        // }
        if (dataa.page!=''){
            pageSize = dataa.page;
        }
        /**
         * 第一个ajax获取页数
         */
        $.ajax({
            url:Feng.ctxPath + "/schedule/schedule_list",
            data: dataa,
            dataType:"json",
            success:function(pageMessage){
                laypage.render({
                    elem: 'pageDemo'
                    ,count: pageMessage.count //显示所有条数
                    ,curr: pageSize
                    ,limit: 8
                    ,page: true
                    ,layout: ['count', 'prev', 'page', 'next', 'skip']
                    ,jump: function(obj,first){
                        //设置页面下一页内容
                        dataa['page'] =obj.curr;
                        dataa['limit']= obj.limit;
                        //设置记录页
                        pageSize = obj.curr;

                        $("#pageCount1").text(obj.curr);
                        $("#pageCount1").val(obj.curr);

                        $.ajax({
                            url: Feng.ctxPath + "/schedule/schedule_list",
                            data: dataa,
                            dataType:"json",
                            success:function (nextPageMessage) {
                                $('#frameName').empty();
                                if (nextPageMessage.count==0){
                                    var html ='';
                                    html += '<div class="layui-col-lg12 layui-col-md12" style="text-align:center;">';
                                    html += '    <img src="/assets/null.jpg" style="height: 21rem;width: 33rem;">';
                                    html += '</div>';
                                    $('#frameName').append(html);
                                }else {

                                    layui.each(nextPageMessage.data,function (index,item) {
                                        var lock = index + 'lock';
                                        var editId = index + 'edit';
                                        var str='';
                                        str += '<div class="layui-col-xs12 layui-col-sm6 layui-col-md3">';
                                        str += '    <div class="myui-card">';
                                        str += '        <div class="layui-card-header">'+ item.scheduleName +'<span class="pull-right">';
                                        if (item.publicStatus =="ENABLE") {
                                            if (item.postStatus =="DISABLE") {
                                                str += '            <i class="layui-icon layui-icon-log" id=' + lock+'></i>';
                                            }
                                            str += '            <i class="layui-icon layui-icon-upload" id=' + lock+'></i></span></div>';
                                        }else {
                                            if (item.postStatus =="DISABLE") {
                                                str += '            <i class="layui-icon layui-icon-log" id=' + lock+'></i>';
                                            }
                                            str += '            <i class="layui-icon layui-icon-password" id=' + lock+'></i></span></div>';
                                        }
                                        str += '        <div class="layui-card-body">';
                                        str += '            <p class="myFont" id="scheduleName">';
                                        str +=  item.description!=""?item.description:"它还没有描述哦～～";
                                        str += '            </p>';
                                        str += '        </div>';
                                        str += '        <ul class="icon">';
                                        str += '            <li id ="viewSchedule' + index +'"><a><i class="layui-icon layui-icon-date"></i></a></li>';
                                        str += '            <li id ="editSchedule' + index +'"><a><i class="layui-icon layui-icon-edit"></i></a></li>';
                                        if(item.postStatus =="DISABLE"){
                                            str += '            <li id ="releSchedule' + index +'"><a><i class="layui-icon layui-icon-share"></i></a></li>';
                                        }
                                        str += '            <li id ="deleteSchedule' + index +'"><a><i class="layui-icon layui-icon-delete"></i></a></li>';
                                        str += '        </ul>';
                                        str += '    </div>';
                                        str += '</div>';

                                        $('#frameName').append(str);


                                        /**
                                         * 进入日历框架
                                         */
                                        $('#viewSchedule'+index).click(function () {
                                            location.href = Feng.ctxPath + "/schedule/details?scheduleId=" + item.scheduleId;
                                        });

                                        /**
                                         * 保存后编辑框架
                                         */
                                        $('#editSchedule'+index).click(function () {
                                            layer.open({
                                                type: 2,
                                                title:'编辑'+item.scheduleName+'日程',
                                                area: ['90%', '95%'],
                                                fixed: false, //不固定
                                                maxmin: true,
                                                content: Feng.ctxPath + '/schedule/edit?scheduleId=' + item.scheduleId,
                                                success:function () {
                                                },
                                                end : function() {
                                                    test();
                                                }

                                            });

                                        });

                                        /**
                                         * 发布框架
                                         */
                                        $('#releSchedule' +index).click(function () {
                                            var operation = function () {
                                                var ajax = new $ax(Feng.ctxPath + "/frame/releaseFrame", function () {
                                                    table.reload(MgrUser.tableId);
                                                    Feng.success("发布成功!");
                                                }, function (data) {
                                                    Feng.error("发布失败!" + data.responseJSON.message + "!");
                                                });
                                                ajax.set("frameId", item.frameId);
                                                ajax.start();
                                                //刷新当前页面
                                                location.reload();
                                            };
                                            Feng.confirm("是否发布"+item.frameName+"框架?发布后内容不可修改", operation);
                                        });

                                        /**
                                         * 删除框架
                                         */
                                        $('#deleteSchedule' +index).click(function () {
                                            var operation = function () {
                                                var ajax = new $ax(Feng.ctxPath + "/schedule/schedule_delete", function () {
                                                    table.reload(MgrUser.tableId);
                                                    Feng.success("删除成功!");
                                                    //刷新当前页面
                                                    // location.reload();
                                                    //回调当前页面
                                                    loadSchedule(dataa);
                                                }, function (data) {
                                                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                                                });
                                                ajax.set("scheduleId", item.scheduleId);
                                                ajax.start();

                                            };
                                            Feng.confirm("是否删除"+item.scheduleName+"日程?", operation);
                                        });
                                    });
                                }
                            }
                        });
                    }
                });
            }
        });


        // var pageMessage = new $ax(Feng.ctxPath + "/frame/frame_list",dataa,"json").start();

    }

    /**
     * 筛选公开或自己的日程
     */
    form.on('select(publicOrprivate)', function(data) {
        queryData['publicOrprivate'] = data.value;
        pageSize =1;
        queryData['page'] = 1;
        loadSchedule(queryData);
    });

    /**
     * 筛选分享/未分享的日程
     */
    form.on('select(shareOrnotshare)', function(data) {
        queryData['shareOrnotshare'] = data.value;
        pageSize =1;
        queryData['page'] = 1;
        loadSchedule(queryData);
    });

    /**
     * 获取日程排序
     */
    form.on('select(sortBydefault)', function(data) {
        queryData['sortBydefault'] = data.value;
        pageSize =1;
        queryData['page'] = 1;
        loadSchedule(queryData);
    });


    /**
     * 点击查询按钮
     */
    MgrUser.search = function () {

        queryData['publicOrprivate'] = $("#publicOrprivate").val();
        queryData['shareOrnotshare'] = $("#shareOrnotshare").val();
        queryData['sortBydefault'] = $("#sortBydefault").val();
        queryData['nameOrcontent'] = $("#nameOrcontent").val();
        queryData['timeLimit'] = $("#timeLimit").val();
        pageSize =1;
        queryData['page'] = 1;
        loadSchedule(queryData);

    };

    function test () {

        queryData['publicOrprivate'] = $("#publicOrprivate").val();
        queryData['shareOrnotshare'] = $("#shareOrnotshare").val();
        queryData['sortBydefault'] = $("#sortBydefault").val();
        queryData['nameOrcontent'] = $("#nameOrcontent").val();
        queryData['timeLimit'] = $("#timeLimit").val();
        queryData['page'] =$("#pageCount1").val()
        loadSchedule(queryData);

    };

    /**
     * 弹出创建日程框架
     */
    MgrUser.openAddUser = function () {
        // func.open({
        //     title: '创建日程框架',
        //     content: Feng.ctxPath + '/frame/create',
        //     tableId: MgrUser.tableId
        // });
        layer.open({
            type: 2,
            title:'创建日程',
            area: ['90%', '95%'],
            fixed: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/schedule/create'
        });
    };

    /**
     * 弹出合成日程界面
     */
    MgrUser.openCopySchedule = function () {

        layer.open({
            type: 2,
            title:'合并日程',
            area: ['90%', '95%'],
            fixed: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/schedule/schedule_copy'
        });
    };

    /**
     * 弹出发布框架对话框
     */
    MgrUser.releFrame = function (data) {
        layer.open({

        });

    };


    /**
     * 点击编辑用户按钮时
     *
     * @param data 点击按钮时候的行数据
     */
    MgrUser.onEditUser = function (data) {
        func.open({
            title: '编辑用户',
            content: Feng.ctxPath + '/mgr/user_edit?userId=' + data.userId,
            tableId: MgrUser.tableId
        });
    };







    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        max: Feng.currentDate()
    });



    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        MgrUser.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        MgrUser.openAddUser();
    });


    // 发布框架
    $('#btnReleFrame').click(function () {
        MgrUser.releFrame();
    });

    //合并日程按钮点击事件
    $('#btncopy').click(function () {
        MgrUser.openCopySchedule();
    });

});

